<?php
/**
 * 用户控制器
 */

namespace app\api\controller;

use app\api\service\GoodsService;
use app\common\model\Goods;
use app\common\model\GoodsSku;
use app\common\service\TimeService;
use think\response\Json;
use app\api\service\UserService;
use app\common\validate\UserValidate;
use app\api\exception\ApiServiceException;

class GoodsController extends ApiBaseController
{
    // 设置添加用户3秒内无法重复请求
    protected array $throttleAction = [

    ];
    public array $loginExcept = [
        'api/goods/index',
        'api/goods/info',
        'api/goods/getFreight',
        'api/goods/checkBuy',
    ];

    /**
     * 列表
     * @param GoodsService $service
     * @return Json
     */
    public function index(GoodsService $service): Json
    {
        try {
            $data = $service->getList($this->param, $this->page, $this->limit);
            return api_success($data);
        } catch (ApiServiceException $e) {
            return api_error($e->getMessage());
        }
    }


    /**
     * 详情
     *
     * @param GoodsService $service
     * @return Json
     */
    public function info(GoodsService $service): Json
    {
        try {
            $result = $service->getDataInfo($this->id);
            if (empty($result)) {
               return api_error('数据不存在');
            }
            $skuModel = new GoodsSku();
            $list = $skuModel->where('goods_id', $this->id)
                ->where('sku',$result['sku'])
                ->select()->toArray();
            $result['sku_list'] = $list;
            return api_success([
                $result,
            ]);

        } catch (ApiServiceException $e) {
            return api_error($e->getMessage());
        }
    }

    //查询商品运费
    public function getFreight(): Json
    {

        $param = $this->param;

        if (isset($param['data']) && !empty($param['data'])){
            $data = json_decode($param['data']);
        }else{
            return api_error('参数错误');
        }
        if(empty($param['primary_address'])){
            return api_error('参数错误');
        }
        if(empty($param['address'])){
            return api_error('参数错误');
        }
        $model = new Goods();
        $goodsSkuModel = new GoodsSku();
        try {
            $goods_list = [];
            foreach($data as $key => $value){
                $info = $model->where('id', $value->goods_id)->find();
                if (!$info){
                    return api_error('商品不存在');
                }
                $goods_list[$key]['sku'] = (int)$info['sku'];
                $goods_list[$key]['library'] = (int)$info['library'];
                $goods_list[$key]['num'] = $value->num;
                $sku_info = $goodsSkuModel->where('id', $value->sku_id)->find();
                if ($sku_info){
                    $goods_list[$key]['variety_index'] = $sku_info['sku_id'];//规格id
                    $goods_list[$key]['variety_name'] = $sku_info['title'];//规格名称
                }else{
                    $goods_list[$key]['variety_index'] = '';//规格id
                    $goods_list[$key]['variety_name'] = '';//规格名称
                }

            }
            if (count($goods_list) > 0){
                $service = new TimeService('93VGNQRG5MYBU0', 'l07g8r45m89xkak0', 'qb82jd75anjxdxka', 'https://main.miniappss.com');

                $result = $service->getFreight($goods_list,$param['primary_address'],$param['address']);
                return api_success($result);
            }

        } catch (ApiServiceException $e) {
            return api_error($e->getMessage());
        }
    }
    //可售性检查
    public function checkBuy(): Json
    {

        $param = $this->param;

        if (isset($param['data']) && !empty($param['data'])){
            $data = json_decode($param['data']);
        }else{
            return api_error('参数错误');
        }
        if(empty($param['primary_address'])){
            return api_error('参数错误');
        }
        if(empty($param['address'])){
            return api_error('参数错误');
        }
        $model = new Goods();
        $goodsSkuModel = new GoodsSku();
        try {
            $goods_list = [];
            foreach($data as $key => $value){
                $info = $model->where('id', $value->goods_id)->find();
                if (!$info){
                    return api_error('商品不存在');
                }
                $goods_list[$key]['sku'] = $info['sku'];
                $goods_list[$key]['library'] = $info['library'];
                $goods_list[$key]['num'] = $value->num;
                $sku_info = $goodsSkuModel->where('id', $value->sku_id)->find();
                if ($sku_info){
                    $goods_list[$key]['variety_index'] = $sku_info['sku_id'];//规格id
                    $goods_list[$key]['variety_name'] = $sku_info['title'];//规格名称
                }else{
                    $goods_list[$key]['variety_index'] = '';//规格id
                    $goods_list[$key]['variety_name'] = '';//规格名称
                }

            }
            if (count($goods_list) > 0){
                $service = new TimeService('93VGNQRG5MYBU0', 'l07g8r45m89xkak0', 'qb82jd75anjxdxka', 'https://main.miniappss.com');
                $result = $service->getFreight($goods_list,$param['primary_address'],$param['address']);
                return api_success($result);
            }

        } catch (ApiServiceException $e) {
            return api_error($e->getMessage());
        }
    }
    //下单 +可售性检查
    public function createOrder(): Json
    {

        $param = $this->param;

        if (isset($param['data']) && !empty($param['data'])){
            $data = json_decode($param['data']);
        }else{
            return api_error('参数错误');
        }
        if(empty($param['primary_address'])){
            return api_error('参数错误');
        }
        if(empty($param['address'])){
            return api_error('参数错误');
        }
        if(empty($param['consignee'])){
            return api_error('参数错误');
        }
        if(empty($param['mobile'])){
            return api_error('参数错误');
        }

        $model = new Goods();
        $goodsSkuModel = new GoodsSku();
        try {
            $goods_list = [];
            foreach($data as $key => $value){
                $info = $model->where('id', $value->goods_id)->find();
                if (!$info){
                    return api_error('商品不存在');
                }
                $goods_list[$key]['sku'] = $info['sku'];
                $goods_list[$key]['library'] = $info['library'];
                $goods_list[$key]['num'] = $value->num;
                $sku_info = $goodsSkuModel->where('id', $value->sku_id)->find();
                if ($sku_info){
                    $goods_list[$key]['variety_index'] = $sku_info['sku_id'];//规格id
                    $goods_list[$key]['variety_name'] = $sku_info['title'];//规格名称
                }else{
                    $goods_list[$key]['variety_index'] = '';//规格id
                    $goods_list[$key]['variety_name'] = '';//规格名称
                }

            }
            if (count($goods_list) > 0){
                $service = new TimeService('93VGNQRG5MYBU0', 'l07g8r45m89xkak0', 'qb82jd75anjxdxka', 'https://main.miniappss.com');
                $result = $service->getFreight($goods_list,$param['primary_address'],$param['address']);
                return api_success($result);
            }

        } catch (ApiServiceException $e) {
            return api_error($e->getMessage());
        }
    }
    //订单查询
    //取消订单
}
